<?php
    class Student_change_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        /**
         * 获取异动数据
         * @param  [type] $date      [description]
         * @param  [type] $school_id [description]
         * @return [type]            [description]
         */
        function get_change_list($school_id, $limit = 0, $offset = 10, $conditions = array()){
            $where = "lea.school_id='{$school_id}'";
            if(!empty($conditions['student'])){
                $where .= " AND stu.name LIKE '%{$conditions['student']}%' ";
            }
            $sql = "select SQL_CALC_FOUND_ROWS lea.*,stu.name as student_name,cla.class_name,campus.name as campus_name from {$this->db->dbprefix}o2o_student_change lea
                left join {$this->db->dbprefix}o2o_student stu on stu.student_id = lea.student_id
                left join {$this->db->dbprefix}o2o_class cla on cla.id = lea.class_id
                left join {$this->db->dbprefix}o2o_school_campus campus on campus.id=cla.campus

                where $where limit $limit, $offset ";
            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }

        /**
         * 添加异动记录
         * @param [type] $school_id [description]
         * @param [type] $leave      [description]
         */
        function add_change($school_id,$change){
          $change_log = "";

          if($change['change_type']==5){
            if($change['campus'] == $change['new_campus']){
              return 0;
            }
            //查新校区
            $sql = "SELECT name from {$this->db->dbprefix}o2o_school_campus WHERE id in(".$change['campus'].", ".$change['new_campus'].")";
            $result = $this->db->query($sql)->result_array();

            $change_log = "转校区：从【{$result[0]['name']}】转到【{$result[1]['name']}】";
            // print_r($change_log);exit;
          }else if($change['change_type']==1){
            if($change['class_id'] == $change['new_class']){
              return 0;
            }else if(empty($change['new_class'])){
              return 0;
            }
            //查询班级
            $sql = "SELECT class_name from {$this->db->dbprefix}o2o_class WHERE id in({$change['class_id']}, {$change['new_class']})";
            $result = $this->db->query($sql)->result_array();
            $change_log = "转班：从【{$result[0]['class_name']}】到【{$result[1]['class_name']}】";
          }

            //事务开始
            $this->db->trans_start();
            //查询

            //1.添加异动记录
            $chg = array(
                'student_id' => $change['student_id'],
                'school_id' => $school_id,
                'refund_money' => 0,
                'class_id' => $change['class_id'],
                'change_type' => $change['change_type'],
                'remark' => $change['remark'],
                'change_log' => $change_log,
                'add_time' => time()
                );
            // $change['school_id'] = $school_id;
            // $change['refund_money'] = 0;
            // $change['add_time'] = time();
            $this->db->insert('o2o_student_change',$chg);

            //2.判断异动类型
            if($change['change_type']==1){
                //转班，更改班级
                $new_class_stu = array('class_id' => $change['new_class']);
                $this->db->update('o2o_class_student',$new_class_stu,array('student_id' => $change['student_id'],'class_id'  => $change['class_id']));
                

            }elseif($change['change_type']==5){
                //转校，更改校区
                $new_stu = array('school_campus_id' => $change['new_campus']);
                $this->db->update('o2o_school_student', $new_stu, array('student_id' => $change['student_id'] ));

            }

            $this->db->trans_complete();
            return 1;
        }




    }
